 
layui.define(['element'], function(exports){
	var $ = layui.$
	  , $body = $('body')
	  ,element = layui.element
	  ,admin = layui.admin
	  ,setter = layui.setter
	  ,common = layui.common
	  ,index = layer.msg('加载中', {
          icon: 16
          ,shade: 0.01
        });
	
	var device = layui.device();
	if(device.os == 'windows'){//判断是电脑
    	$('style[mainStyle]').append('div,td,input,span,button,a,ul li,i,cite{font-size:14px!important;}');
	}else{//判断是平板
		if(window.screen.width > 1366){
			$('style[mainStyle]').append('div,td,input,span,button,a,ul li,i,cite{font-size:18px!important;}'
	    			+'.layui-side-menu .layui-nav .layui-nav-item a{height: 50px !important; line-height: 50px !important;}');
		}else{
			$('style[mainStyle]').append('div,td,input,span,button,a,ul li,i,cite{font-size:14px!important;}');
		}
	}

	common.reSizeMeIndex(92,'layadmin-iframe');
	$(window).resize(function() {
		 common.reSizeMeIndex(92,'layadmin-iframe');
	});
	  admin.req({
	        url: setter.host+'/main/loadOnceMenuInfo'
	        ,type:'get'
	        ,done: function(res){
	          //请求成功后
	          	layer.close(index); 
	          if(res.status == setter.response.statusCode.status_Success){
	          //登入成功的提示与跳转
	          	  loadMenu(res.data);
	          	  element.render('nav');
	          }else{
	        	  layer.msg(res.msg);
	          }
	        }
	      });
	  
	  $body.on('click', '*[own_href]', function(){
		  var othis = $(this)
		  ,value = othis.attr('own_href')
		  ,index = layer.msg('加载菜单中', {
	          icon: 16
	          ,shade: 0.01
	        });
		  
		  for (var i = 1; i < $('#topMenu').children("li").length; i++) {
	            $('#topMenu').children("li").attr('class','layui-nav-item layui-hide-xs');
	        }
	      othis.parent().attr('class','layui-nav-item layui-nav-item-show');
		    admin.req({
		        url: setter.host+'/main/loadOnceMenuInfoById' 
		        ,type:'post'
		        ,data:JSON.stringify({'menuId':value})
		        ,contentType: "application/json; charset=utf-8"
		        ,dataType:'json'
		        ,done: function(res){
		          //请求成功后
		          	layer.close(index); 
		          if(res.status == setter.response.statusCode.status_Success){
		          //登入成功的提示与跳转
		          	  loadLeftMenu(res.data);
		          	  element.render('nav');
		          }else{
		        	  layer.msg(res.msg);
		          }
		        }
		      });
		  });
	  function loadMenu(data){
		  for (var i = 1; i < $('#topMenu').children("li").length; i++) {
            $('#topMenu').children("li").eq(i).remove();
            //$('#topMenu').children("li").attr('class','layui-nav-item layui-hide-xs');
		  }
		  layui.each(data, function(inx,val){
			  if(inx == 0)
				  $('#topMenu').append(' <li class="layui-nav-item layui-nav-item-show"><a style="cursor: pointer" own_href="'+val.menuId+'""><i class="layui-icon '+val.icon+'"> '+val.name+'</i></a></li>');
			  else
				  $('#topMenu').append(' <li class="layui-nav-item layui-hide-xs"><a style="cursor: pointer" own_href="'+val.menuId+'""><i class="layui-icon '+val.icon+'"> '+val.name+'</i></a></li>');
			  if(val.hasOwnProperty("children")){
				  loadLeftMenu(val.children);
			  }
		  });
	  }
	  //加载左侧第一层菜单
	  function loadLeftMenu(menuList){
		  $('#LAY-system-side-menu').find('li').remove();
		  layui.each(menuList,function(inx1,val1){
			  var menu = '<li data-name="'+val1.menuId+'" class="layui-nav-item">';
			  var more = val1.haveChild?'<span class="layui-nav-more"></span>':'';
			  var first = '<a href="javascript:;" lay-tips="'+val1.name+'" >  <i class="layui-icon '+val1.icon+'"></i> <cite>'+val1.name+'</cite>'+more+'</a>';
			  //如果有第二层菜单就加载
			  var second1 = getChild(val1);
			  var first1 = first;
			  var menu1 = menu+first1+'</li>';
			  $('#LAY-system-side-menu').append(menu1);
			  
		  })
	  }
	  
	  function getChild(val){
		  if(val.haveChild){
			   var result = '';
			   var second =' <dl class="layui-nav-child">';
			   layui.each(val,function(inx3,val3){
				  var clickNo = '<a href="javascript:;">';
				  var href = '';
				  if(val3.type=='window')
					  href = 'window-href';
				  else if(val3.type=='newWindow')
					  href = 'newWindow-href';
				  else if(val3.type=='menu'||val3.type=='link')
					  href = 'href="javascript:;" lay-href';
				  var moves = href=='window-href'||href=='newWindow-href'?'style="cursor: pointer" ':'';
				  var click = '<a '+moves+href+'="'+val3.url+'">'+val3.name;
				  var last = val3.haveChild?clickNo+val3.name+'<span class="layui-nav-more"></span>':click;
				  second += '<dd data-name="'+val3.menuId+'" > '+last+'</a></dd>';
				  
			  });
			  result = second+'</dl>';
		  }
		  return result;
	  }

	  $body.on('click', '*[newWindow-href]', function(){
		  var othis = $(this)
		  ,value = othis.attr('newWindow-href')
		  ,name = othis.text()
		  window.open(value,'_blank');
	  });
	//大大的弹出窗
	  $body.on('click', '*[window-href]', function(){
		var othis = $(this)
			,value = othis.attr('window-href')
			,name = othis.text()
		layer.open({
			type: 2,
			title: name,
			shade: false,
			maxmin: true,
			area: ['90%', '90%'],
			content: value
		});
	});
	  setHomePage();
	  //设置不同的首页
	  function setHomePage(){
		  var jobnumber = sessionStorage.getItem('jobnumber') == null?"":sessionStorage.getItem('jobnumber');
		   //门店权限跳转公告
		    if(jobnumber != '' && jobnumber.length == 4){
		    	$('#LAY_app_tabsheader li').attr('lay-id',setter.host+'/notice_info/toShop');
		    	$('#LAY_app_body iframe').attr('src',setter.host+'/notice_info/toShop');
		    }else{//剩余权限不做处理
		    	
		    }
	  }

  //对外暴露的接口
  exports('main', {});
});